
#ifndef IMAGEDOWNLOAD_H
#define IMAGEDOWNLOAD_H

#include <QObject>
#include <QMap>
#include <QCache>
#include <QPixmap>
#include <QNetworkAccessManager>
#include <QPointer>
#include <QUrl>

namespace ImageDownload {


class IconManager : public QObject
{
    Q_OBJECT
public:
    static IconManager* instance(QObject *parent = 0);

    void imageGet( const QString &imageUrl );
    bool contains( const QString &imageUrl ) const;
    QPixmap* imageFromUrl( const QString &imageUrl ) const;

protected:
    IconManager( QObject *parent = 0 );
    ~IconManager();

Q_SIGNALS:
    // The change notification signals of the properties
    void imageReady();
    void imageChanged();
    void loadingChanged();
    void imageReadyForUrl(const QString &path);

private slots:
    void requestFinished(QNetworkReply *reply);

private:
    QUrl redirectUrl(const QUrl& possibleRedirectUrl,
                     const QUrl& oldRedirectUrl) const;
    QNetworkAccessManager* createNetworkAccessManager();

    void insertRedirectUrlMap(const QString &oldUrl, const QString &newUrl);
    QString getOriginalUrl(const QString &url);

private:
     static IconManager *m_instance;

    QMap<QString, QNetworkAccessManager*> m_connections;
    QCache<QString, QPixmap> m_imageCache;

    QString m_originalUrlString;
    QUrl m_originalUrl;
    QUrl m_urlRedirectedTo;
    QUrl m_baseUrl;

    QMap<QString, QString> m_redirectUrlMap;
};

} // namespace ImageDownload
#endif //IMAGEDOWNLOAD_H
